package logic

import (
	"context"
	"fmt"
	"wechat/models/mysql"

	"wechat/userrpc/internal/svc"
	"wechat/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type FriendDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFriendDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FriendDelLogic {
	return &FriendDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *FriendDelLogic) FriendDel(in *userrpc.FriendDelRequest) (*userrpc.FriendDelResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	mysql.DB.Where("id = ?", in.UserId).Find(&user)

	if user.ID == 0 {
		return nil, fmt.Errorf("当前用户不存在")
	}

	var friend mysql.Friend

	mysql.DB.Debug().Where("user_id = ?", in.UserId).Where("friend_id = ?", in.FriendId).Find(&friend)

	if friend.ID == 0 {
		return nil, fmt.Errorf("对方不是你好友不允许删除")
	}

	mysql.DB.Debug().Delete(&friend)

	return &userrpc.FriendDelResponse{}, nil
}
